﻿using ClassLibrary1;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHHelper;
using xbd.ControlLib;

namespace MYZNMarking
{
    public partial class FrmParamSet : Form
    {
        public static Action<bool[], int[]> ParamAction { get; set; }

        public FrmParamSet()
        {
            InitializeComponent();
            this.dgv_Main.AutoGenerateColumns = false;
            TotalParam = GetAllGroups();
            RefreshGroup();
        }
        private List<ParamSet> TotalParam = new List<ParamSet>();
        private string groupPath = Application.StartupPath + "\\Config\\Paramset.xlsx";
        private bool[] bools = new bool[18];
        private int[] ints = new int[5];

        private void btn_Write_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (var item in this.panel1.Controls.OfType<CheckBox>())
                {
                    int index = Convert.ToInt32(item.Tag);
                    bools[index] = !item.Checked;
                }
                ints[0] = Convert.ToInt32(txt_Delay1.Text) / 100;
                ints[1] = Convert.ToInt32(txt_Delay2.Text) / 100;
                ints[2] = Convert.ToInt32(txt_Delay3.Text) / 100;
                MessageBox.Show("参数写入成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Program.Loal = this.txt_ProductName.Text;       
            ParamAction?.Invoke(bools, ints);
        }
        private List<ParamSet> GetAllGroups()
        {
            try
            {
                return MiniExcel.Query<ParamSet>(groupPath).ToList();
            }
            catch (Exception)
            {
                return new List<ParamSet>();
            }

        }
        /// <summary>
        /// 更新通信组集合
        /// </summary>
        private void RefreshGroup()
        {
            if (TotalParam != null && TotalParam.Count > 0)
            {
                this.dgv_Main.DataSource = null;
                this.dgv_Main.DataSource = TotalParam;
            }

        }
        /// <summary>
        /// 判断组名是否存在
        /// </summary>
        /// <param name="groupName"></param>
        /// <returns></returns>
        private bool IsGroupNameExits(string productName)
        {
            return TotalParam.FindAll(C => C.ProductName == productName).ToList().Count() > 0;
        }
        private void btn_Save_Click(object sender, EventArgs e)
        {

            string productName = this.txt_ProductName.Text.Trim();
            if (productName.Length == 0)
            {
                MessageBox.Show("变量名称不能为空！");
                return;
            }
            if (IsGroupNameExits(productName))
            {
                MessageBox.Show("变量名称已经存在！");
                return;
            }
            TotalParam.Add(new ParamSet()
            {
                ProductName = productName,
                Delay_1 = txt_Delay1.Text,
                Delay_2 = txt_Delay2.Text,
                Delay_3 = txt_Delay3.Text,
                P00 = this.P00N.Checked,
                P01 = this.P01N.Checked,
                P02 = this.P02N.Checked,
                P03 = this.P03N.Checked,
                P04 = this.P04N.Checked,
                P05 = this.P05N.Checked,
                P06 = this.P06N.Checked,
                P07 = this.P07N.Checked,
                P08 = this.P08N.Checked,
                P09 = this.P09N.Checked,
                P10 = this.P10N.Checked,
                P11 = this.P11N.Checked,
                P12 = this.P12N.Checked,
                P13 = this.P13N.Checked,
                P14 = this.P14N.Checked,
                P15 = this.P15N.Checked,
            });

            try
            {
                MiniExcel.SaveAs(groupPath, TotalParam, overwriteFile: true);//默认参数不写，最后一个参数可以改
                //刷新数据
                RefreshGroup();
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加通信组失败！" + ex.Message);
            }


        }

        private void btn_Revise_Click(object sender, EventArgs e)
        {
            string productName = this.txt_ProductName.Text.Trim();
            if (!IsGroupNameExits(productName))
            {
                MessageBox.Show("通信组名称不存在!");
                return;
            }
            var group = TotalParam.Find(c => c.ProductName == productName);
            group.ProductName = productName;
            group.Delay_1 = txt_Delay1.Text;
            group.Delay_2 = txt_Delay2.Text;
            group.Delay_3 = txt_Delay3.Text;
            group.P00 = this.P00N.Checked;
            group.P01 = this.P01N.Checked;
            group.P02 = this.P02N.Checked;
            group.P03 = this.P03N.Checked;
            group.P04 = this.P04N.Checked;
            group.P05 = this.P05N.Checked;
            group.P06 = this.P06N.Checked;
            group.P07 = this.P07N.Checked;
            group.P08 = this.P08N.Checked;
            group.P09 = this.P09N.Checked;
            group.P10 = this.P10N.Checked;
            group.P11 = this.P11N.Checked;
            group.P12 = this.P12N.Checked;
            group.P13 = this.P13N.Checked;
            group.P14 = this.P14N.Checked;
            group.P15 = this.P15N.Checked;
            try
            {
                MiniExcel.SaveAs(groupPath, TotalParam, overwriteFile: true);
                //刷新数据
                RefreshGroup();
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改通信组失败！" + ex.Message);
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            string productName = this.txt_ProductName.Text.Trim();
            if (!IsGroupNameExits(productName))
            {
                MessageBox.Show("通信组名称不存在!");
                return;
            }
            try
            {
                TotalParam.RemoveAll(c => c.ProductName == productName);//名字一样的删除掉
                //刷新数据
                RefreshGroup();
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除通信组失败！" + ex.Message);
            }
        }
        private void dgv_Main_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint((DataGridView)sender, e);//添加行号
        }

        /// <summary>
        /// 点击之后自动更新界面数值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_Main_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                var paramSet = TotalParam[e.RowIndex];
                UpdateGroup(paramSet);
            }
        }
        private void UpdateGroup(ParamSet paramSet)
        {
            if (paramSet != null)
            {
                this.txt_ProductName.Text = paramSet.ProductName;
                this.txt_Delay1.Text = paramSet.Delay_1.ToString();
                this.txt_Delay2.Text = paramSet.Delay_2.ToString();
                this.txt_Delay3.Text = paramSet.Delay_3.ToString();
                this.P00N.Checked = paramSet.P00;
                this.P01N.Checked = paramSet.P01;
                this.P02N.Checked = paramSet.P02;
                this.P03N.Checked = paramSet.P03;
                this.P04N.Checked = paramSet.P04;
                this.P05N.Checked = paramSet.P05;
                this.P06N.Checked = paramSet.P06;
                this.P07N.Checked = paramSet.P07;
                this.P08N.Checked = paramSet.P08;
                this.P09N.Checked = paramSet.P09;
                this.P10N.Checked = paramSet.P10;
                this.P11N.Checked = paramSet.P11;
                this.P12N.Checked = paramSet.P12;
                this.P13N.Checked = paramSet.P13;
                this.P14N.Checked = paramSet.P14;
                this.P15N.Checked = paramSet.P15;


            }
        }
        private void dgv_Main_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)//保证有数据
            {

                object value = this.dgv_Main.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 6 || e.ColumnIndex == 7 || e.ColumnIndex == 8 || e.ColumnIndex == 9 || e.ColumnIndex == 10
                    || e.ColumnIndex == 11 || e.ColumnIndex == 12 || e.ColumnIndex == 13 || e.ColumnIndex == 14 || e.ColumnIndex == 15 || e.ColumnIndex == 16 || e.ColumnIndex == 17 || e.ColumnIndex == 18 || e.ColumnIndex == 19 || e.ColumnIndex == 20)
                {
                    if (value != null)
                    {
                        e.Value = value.ToString() == "True" ? "启用" : "禁用";
                    }
                }
                if (value == null || value.ToString().Length == 0)
                {
                    e.Value = "-----";
                }
            }
        }
    }
}
